package com.tongcheng.Controller;

import com.tongcheng.ResultDate;
import com.tongcheng.commons.Contants;
import com.tongcheng.pojo.Activity;
import com.tongcheng.pojo.Activity_share;
import com.tongcheng.service.Activitysevice;
import com.tongcheng.service.Couponssevice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.lang.Long;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/coupons")
@Api(value = "用户和商家操作优惠券",tags = "用户和商家操作优惠券")
public class UserCouponsController {
    @Autowired
    private Couponssevice couponssevice;
    @ApiOperation(value = "用户领取优惠券",notes = "根据userid用户领取优惠券", response = ResultDate.class)
    @RequestMapping(value = "/userReceiveCoupons",method = RequestMethod.GET,produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultDate userReceiveCoupons(Integer userId,Integer coupons_id){
       List<Map> result2=couponssevice.userCouponsQeury(userId);
       ResultDate resultDate=new ResultDate();
       if(result2.size()!=0){
           resultDate.setCode(Contants.FAILURE);
           resultDate.setMsg("失败,用户已有优惠劵");
       }else {
           int result=couponssevice.userReceiveCoupons(userId,coupons_id);
           if (result>0){
               resultDate.setCode(Contants.SUCCESS);
               resultDate.setMsg("添加成功");
            }else {
               resultDate.setCode(Contants.FAILURE);
               resultDate.setMsg("添加失败");
             }
       }

        return resultDate;
    }
    @ApiOperation(value = "用户查看自己优惠券",notes = "根据userid用户查看自己优惠券", response = ResultDate.class)
    @RequestMapping(value = "/userSeeCoupons",method = RequestMethod.GET,produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultDate userSeeCoupons(Integer userId){
        List<Map> mapList=couponssevice.userSeeCoupons(userId);
        ResultDate resultDate=new ResultDate();
        if(mapList.size()!=0){
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("查询成功");
            resultDate.setData(mapList);
        }else {
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("查无数据");
        };
        return resultDate;
    }
    @ApiOperation(value = "商家查看自己优惠券",notes = "根据merchant_id商家查看自己优惠券", response = ResultDate.class)
    @RequestMapping(value = "/merchantSeeCoupons",method = RequestMethod.GET,produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultDate merchantSeeCoupons(Long merchant_id){
        List<Map> mapList=couponssevice.merchantSeeCoupons(merchant_id);
        ResultDate resultDate=new ResultDate();
        if(mapList.size()!=0){
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("查询成功");
            resultDate.setData(mapList);
        }else {
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("查无数据");
        }
        return resultDate;
    }
    @ApiOperation(value = "商家领取优惠券",notes = "根据merchant_id商家领取优惠券", response = ResultDate.class)
    @RequestMapping(value = "/merchantReceiveCoupons",method = RequestMethod.GET,produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultDate merchantReceiveCoupons(Long merchant_id,Integer coupons_id){
        List<Map> result2=couponssevice.merchantCouponsQeury(merchant_id);
        ResultDate resultDate=new ResultDate();
        if(result2.size()!=0){
            resultDate.setCode(Contants.FAILURE);
            resultDate.setMsg("失败,商家已有优惠劵");
        }else {
            int result=couponssevice.merchantReceiveCoupons(merchant_id,coupons_id);
            if (result>0){
                resultDate.setCode(Contants.SUCCESS);
                resultDate.setMsg("添加成功");
            }else {
                resultDate.setCode(Contants.FAILURE);
                resultDate.setMsg("添加失败");
            }
        }
        return resultDate;
    }
}
